# -*- coding: utf-8 -*-
import sys,os,re
from pipe import Pipe
import threading
from req import  Req
import Util#Util.getDict
import random
from factoryReq import  ReqFactory
from mongoBase import MongoDBBase
from factoryFetcher import Fetcher
import units
class UnitFactory(threading.Thread):
    def __init__(self, factory_name, fetcher_to_unit_pipe,unit_to_saver_pipe):
        threading.Thread.__init__(self, name=factory_name)
        self._factory_name = factory_name
        self.fetcher_to_unit_pipe = fetcher_to_unit_pipe
        self.unit_to_saver_pipe = unit_to_saver_pipe
    def run(self):
        while True:
            pipe_is_empty=self.fetcher_to_unit_pipe.empty()
            if pipe_is_empty is True:
                print("######Thread %s is empty ,break!"%self.getName())
                break
            req=self.fetcher_to_unit_pipe.get()
            print("######Thread %s START a job,processed Req: %d" % (self.getName(), req._id))
            self.working(req)
            self.unit_to_saver_pipe.put(req)
            print("######Thread %s FINISHED a job to processed Req: %d" % (self.getName(),req._id))
            print("-------")
            print("Req: %d" %req._id)
            print(req.data_info)
            print("------")
        print("######Thread %s exit" % self.getName())
    def working(self, req):
        if (type(req) is not Req):
            raise Exception
        unit=self.get_unit(req)
        # add data_info of Req this step
        print(type(unit))
        unit.excute()
        return req
    def get_unit(self,req):
        unit_name=req.unit_info["unit_name"]
        if unit_name=='SampleJudge':
            unit=units.Unit_SampleJudge(req)
            #print "Get the unit :%s"%unit_name
            return unit

if __name__ == '__main__':
    myReq_list=[]
    for i in range(5):
        input_info={}
        input_info["unit_name"]="SampleJudge"
        input_info["dataset_from_commit"]={}
        input_info["dataset_from_commit"]["uid_pid_tid_ans_list"]=[]
        for j in range(10):
            input_info["dataset_from_commit"]["uid_pid_tid_ans_list"].append(Util.getDict(
                uid=i+1,pid=100000001+j//4,tid=j%4+1,ans=random.choice([-1,0,1]),ase=0))
        print(input_info)
        req=Req(id=i+1,input_info=input_info)
        myReq_list.append(req)

    user_to_fetcher_pipe=Pipe()
    myReqFactory=ReqFactory("myReqFactory",user_to_fetcher_pipe)
    for r in myReq_list:
        myReqFactory.work(r)
    for r in myReq_list:
        print (r.fetch_info)

    myconn = MongoDBBase()

    fetcher_to_unit_pipe = Pipe()
    myFetcher1 = Fetcher("myFetcher1", user_to_fetcher_pipe=user_to_fetcher_pipe,
    fetcher_to_unit_pipe=fetcher_to_unit_pipe, DB_conn=myconn)
    myFetcher1.start()
    myFetcher1.join()
    '''
       len=fetcher_to_unit_pipe.get_size()
    idx=0
    while idx<len:
        req=fetcher_to_unit_pipe.get()
        print("------")
        print(req.data_info)
        print("------")
        print("######Now fetcher_to_unit_pipe has %d req left"%fetcher_to_unit_pipe.get_size())
        idx+=1
    
    '''


    unit_to_saver_pipe=Pipe()
    myUnitFactory=UnitFactory("myUnit1",fetcher_to_unit_pipe=fetcher_to_unit_pipe, unit_to_saver_pipe=unit_to_saver_pipe)
    myUnitFactory.start()
    myUnitFactory.join()
    '''
        len = unit_to_saver_pipe.get_size()
    idx = 0
    while idx < len:
        req = unit_to_saver_pipe.get()
        print("------")
        print(req.data_info)
        print("------")
        print("######Now unit_to_saver_pipe has %d req left" % unit_to_saver_pipe.get_size())
        idx += 1
    
    '''
